package cn.xorange.commons.utils.Collect;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

public class CollectUtils {
    private CollectUtils(){}


    public static boolean isEmpty(Collection<?> collection){
        if(Objects.nonNull(collection)){
            return collection.isEmpty();
        }
        return true;
    }
    public static boolean isNotEmpty(Collection<?> collection){
        return !isEmpty(collection);
    }
    public static boolean isEmpty(Map<?, ?> map){
        if (Objects.nonNull(map)){
            return map.isEmpty();
        }else {
            return true;
        }
    }
    public static boolean isNotEmpty(Map<?, ?> map){
        return !isEmpty(map);
    }

    /**
     * 集合相减
     * @param coll1 集合1
     * @param coll2 集合2
     * @param <T> 实体
     * @return 集合1减去集合2
     */
    public static <T> Collection<T> subtract(Collection<T> coll1, Collection<T> coll2) {
        Collection<T> result = ObjectUtil.clone(coll1);
        if (null == result) {
            result = CollUtil.create(coll1.getClass());
            result.addAll(coll1);
        }
        result.removeAll(coll2);
        return result;
    }

    /**
     * 判断给定的collection列表中是否包含数组array 判断给定的数组array中是否包含给定的元素value
     *
     * @param collection 给定的集合
     * @param array 给定的数组
     * @return boolean 结果
     */
    public static boolean containsAny(Collection<String> collection, String... array)
    {
        if (isEmpty(collection) || Objects.isNull(array) || array.length == 0) {
            return false;
        }
        else {
            for (String str : array) {
                if (collection.contains(str)) {
                    return true;
                }
            }
            return false;
        }
    }
}
